FROM ubuntu as php8
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
RUN apt-get update && apt-get -y dist-upgrade
RUN apt-get install -y software-properties-common \
&&  add-apt-repository ppa:apt-fast/stable \
&&  apt-get install -y  apt-fast \
&&  add-apt-repository ppa:ondrej/php  \
&&  apt-get update

RUN apt-fast -y install  php8.1 php8.1-dom  php8.1-dev   php-pear  php8.1-fpm php8.1-mysql php8.1-curl  php8.1-mbstring php8.1-xml php8.1-intl  php8.1-redis php8.1-gd php8.1-zip redis php8.1-mongodb
RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer
RUN apt-fast -y  install git vim zip unzip net-tools wget
RUN apt-fast -y install libreadline-dev libpcre3-dev libssl-dev perl zlib1g-dev
RUN cd /usr/local/src \
&& wget https://openresty.org/download/openresty-1.19.9.1.tar.gz  \
&& tar xzvf openresty-1.19.9.1.tar.gz \
&& cd openresty-1.19.9.1/ \
&& ./configure && make && make install

RUN ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/sbin/nginx \
&& mkdir /usr/local/openresty/nginx/conf/sites

ARG src=/usr/local/src
ARG node_version=v16.13.1
RUN cd $src \
&& wget https://npmmirror.com/mirrors/node/${node_version}/node-${node_version}-linux-x64.tar.xz \
&& tar xf  node-${node_version}-linux-x64.tar.xz \
&& cd node-${node_version}-linux-x64 \
&& ./bin/node -v \
&& ln -s $src/node-${node_version}-linux-x64/bin/npm   /usr/local/bin/ \
&& ln -s $src/node-${node_version}-linux-x64/bin/node   /usr/local/bin/

WORKDIR /www/workspace/
EXPOSE 80
RUN echo  "\
server {\n \
    listen   80; ## listen for ipv4; this line is default and implied\n \
    root /www/workspace/public;\n \
    index index.php index.html index.htm;\n \
    error_log /dev/stdout info;\n \
    access_log /dev/stdout;\n \
    gzip on;\n \
    gzip_min_length 10240;\n \
    gzip_proxied expired no-cache no-store private auth;\n \
    gzip_types text/plain text/css text/xml application/json text/javascript application/x-javascript application/xml;\n \
    location / {\n \
        # First attempt to serve request as file, then \n \
        # as directory, then fall back to index.php \n \
        try_files \$uri \$uri/ /index.php?\$query_string;\n \
    }\n \
    client_max_body_size 50m;\n \
    location ~ \.php$ {\n \
        fastcgi_pass 127.0.0.1:9000;\n \
        fastcgi_index index.php;\n \
        include fastcgi_params;\n \
        fastcgi_buffer_size 128k;\n \
        fastcgi_buffers 4 256k;\n \
        fastcgi_busy_buffers_size 256k;\n \
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n \
        fastcgi_param HTTP_MERGED_X_FORWARDED_FOR \$http_x_forwarded_for;\n \
        fastcgi_param PATH_INFO \$fastcgi_path_info;\n \
    }\n \
        location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {\n \
                expires           5d;\n \
        }\n \
}">/usr/local/openresty/nginx/conf/sites/web.conf \
&& echo "\
#user  nobody;\n \
worker_processes  1;\n \
#error_log  logs/error.log;\n \
#error_log  logs/error.log  notice;\n \
#error_log  logs/error.log  info;\n \
#pid        logs/nginx.pid;\n \
events {\n \
    worker_connections  1024;\n \
}\n \
http {\n \
    include       mime.types;\n \
    default_type  application/octet-stream;\n \

    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '\n \
                     '\$status $body_bytes_sent "\$http_referer" '\n \
                      '"\$http_user_agent" "\$http_x_forwarded_for"';\n \
    access_log  logs/access.log  main;\n \
    sendfile        on;\n \
    keepalive_timeout  65;\n \
    #gzip  on;\n \
	include sites/*;\n \
   }\n \
" > /usr/local/openresty/nginx/conf/nginx.conf \
&& echo  "#!/bin/bash\n\
nginx\n\
php-fpm8.1\n\
tail -f /dev/null\n\
" > /www/start.sh \
&& chmod 777 /www/start.sh \
&& sed -i -e "s|/run/php/php8.1-fpm.sock|127.0.0.1:9000|g" /etc/php/8.1/fpm/pool.d/www.conf \
&& mkdir /run/php
ENTRYPOINT ["/www/start.sh"]


